/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import model.KostumBean;

/**
 *
 * @author rzclau
 */
public class DBKostum {
    
    public DBKostum(){
        
    }
    public Connection createConnection() throws ClassNotFoundException,SQLException
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/rbpl", "root", "");
        return connection;
    }
 //we get values from servlet by passing bean object to insertdetails method
     public int insertKostum(KostumBean bb) throws SQLException, ClassNotFoundException
     {
        try{
        Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("insert into daftar_kostum values(?,?,?,?,?)");
        //set values to prepared statement object by getting values from bean object
        pstmt.setString(1,bb.getID_Kostum());
        pstmt.setString(2,bb.getNamaKostum());
        pstmt.setString(3,bb.getHarga());
        pstmt.setString(4,bb.getKategori());
        pstmt.setString(5,bb.getGambar());
        int i = pstmt.executeUpdate();
        System.out.println("testing");
        return i;
        }
        catch(Exception e)
        {
            System.out.println(e);
            return 0;
        }
     }
   public List getAlldetailsKostum()throws SQLException, ClassNotFoundException
    {
       
        Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("select * from daftar_kostum");
        ResultSet rs = pstmt.executeQuery();
        List list = new ArrayList();
        while(rs.next())
        {
            KostumBean kbean = new KostumBean();
            kbean.setID_Kostum(rs.getString(1));
            kbean.setNamaKostum(rs.getString(2));
            kbean.setHarga(rs.getString(3));
            kbean.setKategori(rs.getString(4));
            kbean.setGambar(rs.getString(5));
            list.add(kbean);

        }
        return list;
    }
   public KostumBean getDetailsKostum(String ID_Kos)throws SQLException, ClassNotFoundException
    {
       //here we will write code to get a single record from database
       Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("select * from daftar_kostum where ID_Kostum=?");
        pstmt.setString(1, ID_Kos);
        ResultSet rs = pstmt.executeQuery();
        
        KostumBean kbean = new KostumBean();
        while(rs.next())
        {
            
            kbean.setID_Kostum(rs.getString(1));
            kbean.setNamaKostum(rs.getString(2));
            kbean.setHarga(rs.getString(3));
            kbean.setKategori(rs.getString(4));
            kbean.setGambar(rs.getString(5));
            
        }
        kbean.setSimpan("Simpan");
        return kbean;
    }
     public void UpateDetailsKostum(KostumBean kbean, String ID_Kos)throws SQLException, ClassNotFoundException
    {
         
        Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("update daftar_kostum set NamaKostum=?, Harga=?, Kategori=?, Gambar=? where ID_Kostum=? ");
        //set values to prepared statement object by getting values from bean object
        pstmt.setString(1,kbean.getID_Kostum());
        pstmt.setString(2,kbean.getNamaKostum());
        pstmt.setString(3,kbean.getHarga());
        pstmt.setString(4,kbean.getKategori());
        pstmt.setString(5,kbean.getGambar());
        pstmt.setString(6,ID_Kos);
        pstmt.executeUpdate();       
         
    }
      public void deleteDetailsKostum(String ID_Kos)throws SQLException, ClassNotFoundException
    {
       //here we will write code to get a single record from database
       Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("delete from daftar_kostum where ID_Kostum=?");
        pstmt.setString(1, ID_Kos);
        pstmt.executeUpdate();       
    }
    
}
